/**
 * 
 */
package component.velocity;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * @author liuboen
 * 
 * since:2012-7-3
 * version:1.0.0
 */
public class TestAA {
	  /** Constant for the file URL protocol.*/
    static final String PROTOCOL_FILE = "file";
	
	
	 static URL locateFromClasspath(String resourceName)
	    {
	        URL url = null;
	        // attempt to load from the context classpath
	        ClassLoader loader = Thread.currentThread().getContextClassLoader();
	        if (loader != null)
	        {
	            url = loader.getResource(resourceName);

	            if (url != null)
	            {
	                System.out.println("Loading configuration from the context classpath (" + resourceName + ")");
	            }
	        }

	        // attempt to load from the system classpath
	        if (url == null)
	        {
	            url = ClassLoader.getSystemResource(resourceName);

	            if (url != null)
	            {
	            	System.out.println("Loading configuration from the system classpath (" + resourceName + ")");
	            }
	        }
	        return url;
	    }
	 
	    public static File fileFromURL(URL url)
	    {
	        if (PROTOCOL_FILE.equals(url.getProtocol()))
	        {
	            return new File(URLDecoder.decode(url.getPath()));
	        }
	        else
	        {
	            return null;
	        }
	    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*URL url = locateFromClasspath("test.properties");
		
		File file = fileFromURL(url);
		
		System.out.println(file.getPath());*/
		TestAA a = new TestAA();
		
		
		URL xmlpath = a.getClass().getClassLoader().getResource("test.properties"); 
		System.out.println(xmlpath); 
		
		
	      System.out.println(Thread.currentThread().getContextClassLoader().getResource("" ).getPath());  
	        
	        System.out.println(TestAA.class .getClassLoader().getResource("" ));  
	  
	        System.out.println(ClassLoader.getSystemResource("" ));  
	        System.out.println(TestAA.class.getResource("" ));  
	        System.out.println(TestAA.class.getResource("/" ));
	        // Class 文件所在路径
	        System.out.println(new  File("/" ).getAbsolutePath());  
	        System.out.println(System.getProperty("user.dir" ));  
	        
		try {
			String path = TestAA.class.getResource("/" ).getPath();
			path = path.substring(1);
			System.out.println(path);
			PropertiesConfiguration config = new PropertiesConfiguration(path + "test.properties");
			
			System.out.println(config.getFileName());
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

	}

}
